<!--categories 
 * @Author: liqi
 * @Date: 2022-10-15 21:44:13
 * @LastEditors: liqi
 * @LastEditTime: 2022-10-25 17:13:30
 * @Description: 
-->
<template>
  <!-- 发布资讯页面 -->
  <div>
    <el-form
      :inline="false"
      :model="params"
      class="demo-form-inline"
      style="width: 100%"
      label-width="50px"
      :rules='rules'
      ref='myForm'
    >
      <el-form-item label="标题" style="width: 60%" prop='title'>
        <el-input v-model="params.title"></el-input>
      </el-form-item>
      <el-form-item label="分类" prop='categoryId'>
        <el-select v-model="params.categoryId" placeholder="请选择">
          <el-option
            v-for="item in categories"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="封皮" prop="cover">
        <el-upload
          class="avatar-uploader"
          :action="uploadFileURL"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="params.cover" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="正文" prop='content'>
        <Tinymce :height="200" v-model="params.content" />
      </el-form-item>
    </el-form>

    <!-- 返回和发布按钮 -->
    <div style="float: right">
      <router-link
        class="goback"
        to="/admin/info/article/List"
        style="margin-right: 10px"
        >返回</router-link
      >
      <el-button type="primary" @click="onSubmit">发布</el-button>
    </div>
  </div>
</template>

<script>
import Tinymce from "@/components/Tinymce";
import { mapGetters } from "vuex";
import { uploadFileURL } from "@/utils/config";
import { post } from '@/utils/request'
export default {
  components: {
    Tinymce,
  },
  data() {
    return {
      // 图片上传路径
      uploadFileURL,
      params: {},
      // 头像上传图片变量
      imageUrl: '',
      rules:{
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        categoryId: [{ required: true, message: '请选择分类', trigger: 'change' }],
        content: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      }
    };
  },
  computed: {
    // 获取栏目数据
    ...mapGetters(["categories"]),
  },
  methods: {
    // 图片上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.params.cover = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 保存和更新
    async onSubmit(){
      this.$refs.myForm.validate(async(valid)=>{
        if(valid){
          let res = await post('/article/saveOrUpdate',this.params)
          this.$notify({
            type:'success',
            message: res.message
          })
          this.$refs.myForm.resetFields()
          this.$router.push({
            path:"List"
          })
        }else{
          return false
        }
      })
    }
  },
  created() {
    this.$store.dispatch('category/queryCategory')
  },
  mounted() {},
};
</script>
<style scoped>
.container {
  display: flex;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 110px;
  height: 110px;
  line-height: 110px;
  text-align: center;
}
.avatar {
  width: 110px;
  height: 110px;
  display: block;
}
.goback {
  font-size: 14px;
  color: #409eff;
}
</style>